package lk.controller;

import lk.exception.ResponseCodeContants;
import lk.exception.RestfulRecord;
import lk.pojo.UserInfo;
import lk.service.LoginService;
import lk.util.SessionUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 */
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Object login(@RequestBody Map<String, String> map) {
        //判空
        if (map != null) {
            //解码并转为json对象
            String loginName = map.get("loginName");
            String password = map.get("password");
            return loginService.login(loginName, password);
        }
        return new RestfulRecord(ResponseCodeContants.SUCCESS_CODE, "请求参数为空！");
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public Object register(@RequestBody UserInfo userInfo) {
        int res = loginService.addUser(userInfo);
        String userId = userInfo.getUserId();
        if (res > 0) {
            return res;
        }
        RestfulRecord record = new RestfulRecord(ResponseCodeContants.ERROR_CODE, "注册失败,学号或手机号已被使用", res);
        return record;
    }

    @RequestMapping(value = "myInfo", method = RequestMethod.GET)
    public Object myInfo(){
        return loginService.findUserByLoginName(SessionUtil.getUserInfo().getLoginName());
    }
}
